using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sirenix.OdinInspector;
using Sirenix.Serialization;

namespace BTEditor
{
    [BTTask]
    public class Wait : Task
    {
        public enum WaitType
        {
            UseFloat,
            UseBlackborad,
            UseFunc
        }

        public override Type GetNodeType()
        {
            return typeof(NPBehave.Wait);
        }

        public override ErrorType GetIsVaild()
        {
            return ErrorType.None;
        }

        protected override void DealField(StringBuilder sb)
        {
            switch (waitType)
            {
                case WaitType.UseFloat:
                    sb.Append($"{seconds.ToArgs()},");
                    sb.Append($"{randomVariance.ToArgs()}");
                    break;
                case WaitType.UseBlackborad:
                    sb.Append($"{BlackboardData.Key},");
                    sb.Append($"{randomVariance.ToArgs()}");
                    break;
                case WaitType.UseFunc:
                    sb.Append($"{func.ToChild()},");
                    sb.Append($"{randomVariance.ToArgs()}");
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        [EnumToggleButtons] [HideLabel] [OdinSerialize] [Title("等待类型")]
        public WaitType waitType;

        [HideLabel] [EnableIf("waitType", WaitType.UseFloat)] [OdinSerialize] [Title("等待时间")]
        public float seconds = 0;

        [HideLabel]
        [OdinSerialize]
        [PropertyOrder(-999)]
        [BoxGroup("Blackboard", CenterLabel = true)]
        [ValueDropdown("BlackboardDataFunc", IsUniqueList = true, DropdownTitle = "选择定义值", DropdownHeight = 500)]
        public BlackboardData BlackboardData;

        [HideLabel]
        [EnableIf("waitType", WaitType.UseFunc)]
        [ShowInInspector]
        [Title("方法")]
        [OdinSerialize]
        [ValueDropdown("ValuesFunction", DropdownTitle = "方法选择", DropdownHeight = 500,
            DisableListAddButtonBehaviour = true)]
        public FloatFunc func;


        [HideLabel] [Title("执行差异")] [BTValue(DealType.OnlyUse)] [OdinSerialize]
        public float randomVariance = 0;

        private IEnumerable<BlackboardData> BlackboardDataFunc()
        {
            return BTEditorProperty.Instance.CurAsset.DefineBlackboard.ToArray();
        }

        private IEnumerable ValuesFunction()
        {
            List<Type> types = GetType().Assembly.GetTypes().ToList();
            return types.Where(x => x.IsSubclassOf(typeof(FloatFunc)))
                .Select(x => Activator.CreateInstance(x))
                .Select(x => new ValueDropdownItem(x.ToString(), x));
        }
    }
}